﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Aulac.Verb.Ems;
using Aulac.Global.Xml;
using Aulac.Rsc.Ems.Tags;
using Aulac.Rsc;
using Aulac.Global.Enums;
using Aulac.Client.Global.Interface;
using Aulac.Global.Common;
using Aulac.Client.Global.Member;
using Aulac.Rsc.Ems;
using System.Data;
using Aulac.Global.Data;

namespace Aulac.Client.Ems.WinForm.Stores
{
    partial class FormRelsMdnStore
    {
        private bool GetVchOutStore(string mVoucherID, VchTypes mVoucherType)
        {
            try
            {
                XmlDataDocument xmlDoc = GlobalMember.AulacConnectSrv.CreateXmlDocument(AulacEmsVerb.MngStore.RecStore.GetVchRecStore, AulacEmsVerb.MngStore.Noun.MngStore);
                XmlProcess.AddValueParamItem(xmlDoc, tagVch.Inv.VchRecInvPrkID, mVoucherID);
                XmlProcess.AddValueParamItem(xmlDoc, tagGeneral.VchOrgID, mVoucherID);
                XmlProcess.AddValueParamItem(xmlDoc, tagGeneral.VchTypeID, mVoucherType);

                XmlDataDocument xmlData = GlobalMember.AulacConnectSrv.ProcessRequest(xmlDoc);
                XmlElement eHeader = XmlProcess.GetVchHeaderElm(xmlData);
                XmlNodeList listNode = XmlProcess.GetListVchLineItem(xmlData);
                foreach (Aulac.Client.Global.Interface.IAulacValueDataCtrl ctr in GetListControlsOnForm())
                    ctr.SetValueToControl(eHeader);
                grdLine.AddDataToControl(listNode);
                for (int i = 1; i < grdLine.grdData.Rows.Count; i++)
                    grdLine.grdData.SetValueCell(i, tagVch.Inv.PriceInExc, grdLine.grdData.GetValueDataCell(i, tagVch.Inv.PriceBuyInExc));
                mVchName = XmlProcess.GetValueAtrb(eHeader, tagVch.Inv.VchName);
                return true;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }            
        }

        private bool SaveRelsMdnStore(FormModes pFormMode, ref XmlDataDocument xmlData)
        {
            try
            {
                string sVerb = (pFormMode == FormModes.NewMode) ? AulacEmsVerb.MngStore.RelsStore.AddVchRelsStore : AulacEmsVerb.MngStore.RelsStore.UpdateVchRelsStore;
                XmlDataDocument xmlDoc = CreateXmlDocument(sVerb, AulacEmsVerb.MngStore.RelsStore.Noun.RelsStore );
                XmlElement eHeader = XmlProcess.AddVchHeaderElm(ref xmlDoc);
                
                foreach (Aulac.Client.Global.Interface.IAulacValueDataCtrl ctr in GetListControlsOnForm())
                    ctr.GetValueToXml(ref eHeader);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.TagStore, TagStores.OutputStore);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.VchName, mVchName);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.VchTypeID, mVchType);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.IsActive, true);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.PartTypeID, cboObjStoreRec.ComboObjectTyeps.ObjTypeID);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.PartPrkID, cboObjStoreRec.ObjPrkID);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.PartName, cboObjStoreRec.ObjName);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.IsCheckQtyLost, mIsCheckInstock);

                if (pFormMode == FormModes.EditMode)
                    XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.VchRelsInvPrkID, mVoucherID);
                
                XmlElement eLines = XmlProcess.AddVchLinesElm(ref xmlDoc);
                int iCountLine = grdLine.grdData.ReadOnly == true ? grdLine.grdData.Rows.Count : grdLine.grdData.Rows.Count - 1;
                for (int i = 1; i < iCountLine; i++)
                {
                    grdLine.grdData.SetValueCell(i, tagVch.Inv.StorePrkID, cboStoreRels.StorePrkID);
                    grdLine.grdData.SetValueCell(i, tagVch.Inv.PriceBuyInExc, grdLine.grdData.GetValueDataCell(i, tagVch.Inv.PriceInExc));
                }
                ((IAulacDataContainerCtrl)grdLine).GetDataToChildNodeXml(eLines);

                // Chuyen nhung gia tri o duoi ve dang khong co dinh dang dau ','
                XmlNodeList mNodeList = XmlProcess.GetListVchLineItem(xmlDoc);
                foreach (XmlElement Elmt in mNodeList)
                {
                    XmlElement eL = Elmt;
                    string sPriceInExc = eL.Attributes[tagVch.Inv.PriceBuyInExc].Value.ToString().Replace(",", ".");
                    string sAmtLineInExc = eL.Attributes[tagVch.Inv.AmtLineInExc].Value.ToString().Replace(",", ".");
                    string sPrimeCostInExc = eL.Attributes[tagVch.Inv.PrimeCostInExc].Value.ToString().Replace(",", ".");
                    string sAmtTaxLineInExc = eL.Attributes[tagVch.Inv.AmtTaxLineInExc].Value.ToString().Replace(",", ".");
                    string sAmtTtlLineInExc = eL.Attributes[tagVch.Inv.AmtTtlLineInExc].Value.ToString().Replace(",", ".");
                    string sAmtPrimeCostLineInExc = eL.Attributes[tagVch.Inv.AmtPrimeCostLineInExc].Value.ToString().Replace(",", ".");

                    XmlProcess.SetAtrb(ref eL, tagVch.Inv.PriceInExc, sPriceInExc);
                    XmlProcess.SetAtrb(ref eL, tagVch.Inv.AmtLineInExc, sAmtLineInExc);
                    XmlProcess.SetAtrb(ref eL, tagVch.Inv.PrimeCostInExc, sPrimeCostInExc);
                    XmlProcess.SetAtrb(ref eL, tagVch.Inv.AmtTaxLineInExc, sAmtTaxLineInExc);
                    XmlProcess.SetAtrb(ref eL, tagVch.Inv.AmtTtlLineInExc, sAmtTtlLineInExc);
                    XmlProcess.SetAtrb(ref eL, tagVch.Inv.AmtPrimeCostLineInExc, sAmtPrimeCostLineInExc);
                }

                xmlData = DataRequest.ProcessRequest(xmlDoc);
                return true;
            }
            catch (Exception ex)
            {
                Aulac.Global.Exceptions.AulacException x = new Aulac.Global.Exceptions.AulacException(ex);
                if (ErrCodes.ErrorCode.Error0010.Equals(x.ErrorCode))
                {
                    XmlNodeList lst = XmlProcess.GetListVchLineItem(x.XmlEcexption);
                    foreach (XmlElement eLine in lst)
                    {
                        DataRow row = grdLine.AulacDataGrid.GetFirstDataRowByKey(tagVch.Inv.ItemPrkID, XmlProcess.GetValueAtrb(eLine, tagVch.Inv.ItemPrkID));
                        DataTableProcess.SetValueDataRow(row, tagVch.Inv.OverInstock, true);
                    }
                    return false;
                }
                else throw ex;

            }
        }
        private string GetNewVchNo(ref string pVchName)
        {
            XmlDataDocument xmlDoc = DataRequest.CreateXmlDocument(Aulac.Verb.Ems.AulacEmsVerb.MngStore.RelsStore.GetNewVchNo, Aulac.Verb.Ems.AulacEmsVerb.MngStore.RelsStore.Noun.RelsStore);
            XmlProcess.AddValueParamItem(xmlDoc, tagVch.Inv.VchTypeID, mVchType);
            XmlDataDocument outDoc = DataRequest.ProcessRequest(xmlDoc);
            pVchName = XmlProcess.GetValueVchHeader(outDoc, tagVch.Inv.VchName);
            if (ValueHelper.isEmpty(pVchName))
                pVchName = Aulac.Client.Data.Ems.Helper.VchHelper.GetVchNameOnServer(mVchType);

            return XmlProcess.GetValueVchHeader(outDoc, tagVch.Inv.VchNo);
        }
    }
}
